#include "view_device_reset.h"
#include "view_device_reset.inc"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "app_timer.h"
#include "times/view_time_0.h"
#include "reset_protect.h"
#include "Motor.h"
#include "bsp.h"
#define LONG_PRESS_MS (3000)
void view_dvice_reset_icon_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    // disp_draw_user_image(58, 28, icon_restart);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 58, 28, FILE_IMG_gj_58_28);
    char buffer[20] = {0};
    uint16_t len = 16;
    memset(buffer, 0, sizeof(buffer));
    sd_ble_gap_device_name_get((uint8_t *)buffer, &len);
    disp_show_user_string_middle(0, 240, 176, buffer, num_hex_12x28_get_bmp);
}
void view_device_reset_confirm_display(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 86, 56, FILE_IMG_gj_86_56);
    if (disp_ctrl.com_data.i_temp[0] == 0)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 19, 126, FILE_IMG_gj_19_126);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 166, 138, FILE_IMG_gj_166_138);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 46, 138, FILE_IMG_gj_46_138);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 141, 126, FILE_IMG_gj_141_126);
    }
}
view_event_result_t view_device_reset_icon_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#error not defined
        return true;
#endif //!<#ifdef EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#error not defined
        return true;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#error not defined
        return true;
#endif //!< #ifdef EVENT_LEVEL_UP
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#error not defined
        return true;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
        display_show(view_time, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    case event_button_front_long_press:
        // reset_protect_set_reset();
        // motor_start(100);
        return view_event_result_handled;
#endif
    case BUTTON_EVENT_LONG_PRESS_KEEP:
        if (param->long_press_millisecond >= LONG_PRESS_MS)
        {
            display_show(view_device_reset_confirm, DISP_MODE_NORMAL);
            button_state_clear();
            // LOG("system_into_poweroff %d", param->long_press_millisecond);
            // motor_start(500);
            // system_into_poweroff();
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
void view_dvice_reset_display(int view)
{
    switch (view)
    {
    case view_device_reset_icon:
        view_dvice_reset_icon_display();
        break;
    case view_device_reset_confirm:
        view_device_reset_confirm_display();
        break;
    }
}

view_event_result_t view_device_reset_confirm_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        disp_ctrl.com_data.i_temp[0] = 0;
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#error not defined
        return true;
#endif //!<#ifdef EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#error not defined
        return true;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#error not defined
        return true;
#endif //!< #ifdef EVENT_LEVEL_UP
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#error not defined
        return true;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
        disp_ctrl.com_data.i_temp[0] = !disp_ctrl.com_data.i_temp[0];
        display_set_update_request(view_device_reset_confirm, 0);
        return view_event_result_handled;
    case event_button_front_long_press:
        return view_event_result_handled;
#endif
    case BUTTON_EVENT_LONG_PRESS_KEEP:
        if (param->long_press_millisecond >= LONG_PRESS_MS)
        {
            button_state_clear();
            if (disp_ctrl.com_data.i_temp[0] == 0)
            {
                motor_start(500);
                system_into_poweroff();
            }
            else
            {
                display_show(view_device_reset_icon, DISP_MODE_NORMAL);
            }
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_device_reset_event(int view, event_param_t const *param)
{
    view_event_result_t result = view_event_result_not_handled;

    switch (view)
    {
    case view_device_reset_icon:
        result = view_device_reset_icon_event(param);
        break;
    case view_device_reset_confirm:
        result = view_device_reset_confirm_event(param);
        break;
    }
    return result;
}
